package com.naxiang.cn.weight;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.naxiang.cn.naxiangbackcard.R;


/**
 * 作者: 赵航
 * 创建时间: 2018/8/7 15:06
 * 描述:
 */
public class CardView extends RelativeLayout {


    private RelativeLayout layout;
    private LinearLayout   mChengecard;
    private ImageView      mBankIcon;
    private TextView       mCardType;
    private EditText       mChargeMoney;
    private Button         mStartChage;

//    private ProgressBar mProgressbar;
//    private TextView    mTextView;

    public CardView(Context context) {
        super(context);
        initView();
    }

    public CardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        layout = (RelativeLayout) inflater.inflate(R.layout.mo_naxiangbackcard_card_layout, this);
        mChengecard = (LinearLayout) layout.findViewById(R.id.chengecard);
        mBankIcon = (ImageView) layout.findViewById(R.id.bankicon);
        mCardType = (TextView) layout.findViewById(R.id.cardtype);
        mChargeMoney = (EditText) layout.findViewById(R.id.chargemoney);
        mStartChage = (Button) layout.findViewById(R.id.startchage);
//        mProgressbar = (ProgressBar) layout.findViewById(R.id.progressbar);
//        ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
//        imageLoader.init(ImageLoaderConfiguration.createDefault(getContext()));

//        mTextView = (TextView) layout.findViewById(R.id.textview);
//        mChengecard.setVisibility(INVISIBLE);
//        mTextView.setVisibility(INVISIBLE);
//        mProgressbar.setVisibility(VISIBLE);

//        //添加图片加载监听
//        imageLoader.displayImage(uri, mBankIcon, new ImageLoadingListener() {
//            @Override
//            public void onLoadingStarted(String s, View view) {
//
//
//            }
//
//            @Override
//            public void onLoadingFailed(String s, View view, FailReason failReason) {
//                mChengecard.setVisibility(INVISIBLE);
//                mProgressbar.setVisibility(INVISIBLE);
//                mTextView.setVisibility(VISIBLE);
//            }
//
//            @Override
//            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//                mChengecard.setVisibility(VISIBLE);
//                mProgressbar.setVisibility(INVISIBLE);
//                mTextView.setVisibility(INVISIBLE);
//            }
//
//            @Override
//            public void onLoadingCancelled(String s, View view) {
//
//            }
//        });

        //吃掉点击事件
        layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        mStartChage.setEnabled(false);


        //设置Input的类型过滤
        mChargeMoney.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    ToastMaked.showToast(getContext(), "第一位数字不能为点");
                    return "";
                }
                if (source.equals("0") && dest.toString().length() == 0) {
                    return "0.";
                }

                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int length = dest.toString().substring(index).length();

                    String money = dest.toString();
                    double chagerMoney = Double.parseDouble(money);
                    if (chagerMoney > 5000.00) {
                        return "";
                    }
                    if (length == 3) {
                        return "";
                    }
                } else {
                    int length = dest.toString().length();
                    if (length > 4) {
                        return "";
                    }
                }
                return null;
            }
        }});


        //设置按钮监听事件
        mChargeMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


                if (s.length() > 0) {
                    mStartChage.setEnabled(true);
                    mChargeMoney.setTextSize(30);
                } else {
                    mStartChage.setEnabled(false);
                    mChargeMoney.setTextSize(18);
                }

                int counter = counter(s.toString(), '.');
                if (counter > 1) {
                    //小数点第一次出现的位置
                    int index = s.toString().indexOf('.');
                    mChargeMoney.setText(s.subSequence(0, index + 1));
                }

                String money = s.toString();
                if (!TextUtils.isEmpty(money)) {
                    double chagerMoney = Double.parseDouble(money);
                    if (chagerMoney > 5000.00) {
                        ToastMaked.showToast(getContext(), "单次金额不能多于5000");
                        mStartChage.setEnabled(false);
                        return;
                    }
                    mStartChage.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }

    public TextView getCardType() {
        return mCardType;
    }

    public EditText getChargeMoney() {
        return mChargeMoney;
    }

    public Button getStartChage() {
        return mStartChage;
    }

    public LinearLayout getChengecard() {
        return mChengecard;
    }

    public ImageView getBankIcon() {
        return mBankIcon;
    }


    /**
     * 统计一个字符在字符串中出现的次数
     *
     * @param s 字符串
     * @param c 字符
     * @return 數量
     */
    public int counter(String s, char c) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }

}
